package com.geeklei.media.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.geeklei.media.ui.common.ErrorEvent;
import com.geeklei.media.ui.controller.MediaController;
import com.geeklei.media.ui.controller.OrientationUpdater;
import com.geeklei.media.ui.widget.GestureSeek;
import com.geeklei.videoplayer.GeekleiVideoView;

import tv.danmaku.ijk.media.player.AbstractMediaPlayer;

/**
 * Created by leixun on 16/6/1.
 */
public class PortraitScreenVideoController extends RelativeLayout implements IVideoLifeCycle, com.geeklei.media.ui.ControllerView.OnControlEventListener {
    String TAG = "PortraitScreenVideoController";
    private RelativeLayout mAnchor;
    private Runnable mAutoDismiss = new Runnable() {
        public void run() {
            com.geeklei.media.ui.PortraitScreenVideoController.this.hideController();
        }
    };
    private ImageView mFullscreen;
    private boolean mIsShowing = false;
    private boolean mIsVideoLoading = false;
    private MediaController mMediaController;
    private Activity mActivity;
    private OrientationUpdater mOrientationUpdater;
    private OnClickListener mOnClickListener = new OnClickListener() {
        public void onClick(View view) {
            if (view == com.geeklei.media.ui.PortraitScreenVideoController.this.mFullscreen && com.geeklei.media.ui.PortraitScreenVideoController.this.mOrientationUpdater != null) {
                com.geeklei.media.ui.PortraitScreenVideoController.this.mOrientationUpdater.requestLandscape();
            }
        }
    };
    private AbstractMediaPlayer mPlayer;
    private GestureSeek mSeek;
    private boolean mShowFullscreenIcon = true;
    private Handler mUiHandler = new Handler(Looper.getMainLooper());
    private IUserEventListener mUserEventListener;

    public PortraitScreenVideoController(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }

    public PortraitScreenVideoController(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public PortraitScreenVideoController(Context context) {
        super(context);
    }

    public void attachMediaPlayer(AbstractMediaPlayer mediaPlayerControl, IUserEventListener userEventListener) {

        this.mPlayer = mediaPlayerControl;
        this.mUserEventListener = userEventListener;
        mMediaController.attachPlayer(mPlayer, userEventListener);
    }

    public void attachActivity(Activity activity, RelativeLayout relativeLayout, OrientationUpdater orientationUpdater) {
        this.mAnchor = relativeLayout;
        this.mActivity = activity;
        this.mOrientationUpdater = orientationUpdater;
    }

    public boolean isShowing() {
        return this.mIsShowing;
    }


    public void setEableFullscreenIcon(boolean flag) {
        this.mShowFullscreenIcon = flag;
        ((Activity) getContext()).runOnUiThread(new Runnable() {
            public void run() {
                if (com.geeklei.media.ui.PortraitScreenVideoController.this.mShowFullscreenIcon) {
                    com.geeklei.media.ui.PortraitScreenVideoController.this.mFullscreen.setVisibility(VISIBLE);
                } else {
                    com.geeklei.media.ui.PortraitScreenVideoController.this.mFullscreen.setVisibility(INVISIBLE);
                }
            }
        });
    }

    protected void onFinishInflate() {
        super.onFinishInflate();
        this.mMediaController = (MediaController) findViewById(R.id.media_controller);
        this.mMediaController.setVisibility(GONE);
        this.mFullscreen = (ImageView) findViewById(R.id.video_portrait_fullscreen);
        this.mFullscreen.setOnClickListener(this.mOnClickListener);
        if (this.mShowFullscreenIcon) {
            this.mFullscreen.setVisibility(VISIBLE);
        } else {
            this.mFullscreen.setVisibility(INVISIBLE);
        }
    }

    public void onTouchMove(int i, float f, float f2) {
        if (i == 2 && isSeekGestureEnable()) {
            adjustSeekStart(f);
        }
    }

    public void onTouchUp(int i) {
        if (i == 2 && isSeekGestureEnable()) {
            adjustSeekEnd();
        }
    }

    public void onTap(int i) {
        if (i == 2 && this.mPlayer != null && !this.mIsVideoLoading ) {
            if (this.mIsShowing) {
                hideController();
            } else {
                showController(false);
            }
        }
    }

    protected void onVisibilityChanged(View view, int i) {
        super.onVisibilityChanged(view, i);
        showStatus();
        refreshViews();
    }

    public void refreshViews() {
        if (this.mPlayer != null) {
            if (this.mIsShowing) {
                showController(true);
            } else {
                hideController();
            }
        }
    }

    public void showController(boolean flag) {
        showStatus();
        if (this.mPlayer != null) {
            if (this.mShowFullscreenIcon) {
                this.mFullscreen.setVisibility(VISIBLE);
            }
            this.mMediaController.setVisibility(VISIBLE);
            resetAutoDismiss();
            this.mIsShowing = true;
        }
    }

    public void hideController() {
        clearAutoDismiss();
        showStatus();
        this.mMediaController.setVisibility(GONE);
        if (this.mShowFullscreenIcon) {
            this.mFullscreen.setVisibility(GONE);
        }
        this.mIsShowing = false;
    }

    protected void onAttachedToWindow() {
        showStatus();
        super.onAttachedToWindow();
        refreshViews();
    }

    protected void onDetachedFromWindow() {
        showStatus();
        super.onDetachedFromWindow();
        hideController();
    }

    public void onEpLoadingStart() {

    }

    public void onVideoLoadingStart(GeekleiVideoView iVideoView) {

    }

    public void onCompletion(GeekleiVideoView iVideoView) {
        hideController();
    }

    public void onPrepared(GeekleiVideoView iVideoView) {
        showStatus();
        this.mIsVideoLoading = false;
    }

    public void onBufferingStart(GeekleiVideoView iVideoView) {
    }

    public void onBufferingEnd(GeekleiVideoView iVideoView) {
    }

    public void onBufferingPercent(GeekleiVideoView iVideoView, int i) {
    }

    public void onCpPluginInstallStart(String str) {
    }

    public void onCpPluginInstallEnd() {
    }

    public void onAdsPlayStart(GeekleiVideoView iVideoView) {
        showController(false);
    }

    public void onAdsPlayEnd(GeekleiVideoView iVideoView) {
        hideController();
    }

    private boolean isSeekGestureEnable() {
        if (this.mIsVideoLoading || this.mPlayer == null || this.mIsShowing) {
            return false;
        }
        return true;
    }

    private void adjustSeekStart(float f) {
        if (this.mSeek == null) {
            this.mSeek = GestureSeek.create(this.mAnchor, this.mMediaController);
        }
        this.mSeek.adjustSeekStart(f);
    }

    private void adjustSeekEnd() {
        if (this.mSeek != null) {
            this.mSeek.adjustSeekEnd();
        }
    }

    private void showStatus() {
    }

    private void resetAutoDismiss() {
        this.mUiHandler.removeCallbacks(this.mAutoDismiss);
        this.mUiHandler.postDelayed(this.mAutoDismiss, 8000);
    }

    private void clearAutoDismiss() {
        this.mUiHandler.removeCallbacks(this.mAutoDismiss);
    }

    @Override
    public void onInfoListener(GeekleiVideoView iVideoView, int what, int extras) {

    }

    @Override
    public void onError(GeekleiVideoView iVideoView, int what, int extras, ErrorEvent errDesc) {

    }
}
